
"use strict";

angular.module('app.user').controller('AllRoleCtrl', function ($scope,$http,$cookieStore) {
 //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [1, 2, 3, 40, 50],
        onChange: function () {
        }
    };
    // $scope.userid = $cookieStore.get('userid');
	$scope.get_list = function(){
		var page_obj = {
			page:$scope.paginationConf.currentPage,
			rows:$scope.paginationConf.itemsPerPage
		}
		$http.post(baseurl+'/list/select/role',page_obj).then(function(res){
			// console.log(res.data);
			$scope.datas = res.data.rows;
			$scope.paginationConf.totalItems = res.data.records;
		})
	}
	$scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.get_list);
    // 添加
	$scope.showAlert = function () {
	    $scope.addobj = {};
	    $(".sureRole").prop('disabled', false);
	}
    $scope.sure_add = function(){
        if($scope.addobj.name == null || $scope.addobj.name == ''){
            new Notify('fail','名称不能为空'); 
            return;
        }
    	$http.post(baseurl+'/data/role',$scope.addobj).then(function(res){
    	    $(".sureRole").prop('disabled', true);
            if (res.data.ret == 1) {
            	 $("#addAlert").modal("hide");
            	 $scope.get_list();
            }else if (res.data.ret != -4){
            	 // console.log("插入出错");
                 new Notify('fail','插入出错'); 
            }
    	});
    }
	// 修改
	$scope.showEditAlert = function(item){
		$scope.editobj = {
           name:item.name,
           des:item.des
        };
        $scope.editid = item.id;
	}
    $scope.sure_edit = function(){
        if($scope.editobj.name == null || $scope.editobj.name == ''){
            new Notify('fail','名称不能为空'); 
            return;
        }
    	$http.put(baseurl+'/data/role?id=='+$scope.editid,$scope.editobj).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
            	 $("#editAlert").modal("hide");
            	 $scope.get_list();
            }else if (res.data.ret != -4){
                 // console.log("插入出错");
                 new Notify('fail','编辑出错'); 
            }
    	});
    }
	// 删除
	$scope.showDelDialog = function(id){
		$scope.delid = id;
	}
	$scope.sure_del = function(){
        // console.log($scope.delid);
    	$http.delete(baseurl+'/data/role?id=='+$scope.delid).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
            	 $("#delAlert").modal("hide");
            	 $scope.get_list();
            }
    	});
	}


});